package q890_findAndReplacePattern;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Solution {
    /*
    跟q205一模一样的题
     */
    public List<String> findAndReplacePattern(String[] words, String pattern) {
        Map<Character, Character> map1 = new HashMap<>();
        Map<Character, Character> map2 = new HashMap<>();
        List<String> res = new ArrayList<>();
        int i = 0;
        for (String word : words) {
            map1.clear();
            map2.clear();
            i = 0;
            while (i < words.length) {
                if ((map1.containsKey(pattern.charAt(i)) && map1.get(pattern.charAt(i)) != word.charAt(i)) || (map2.containsKey(word.charAt(i)) && map2.get(word.charAt(i)) != pattern.charAt(i))) break;
                map1.put(pattern.charAt(i), word.charAt(i));
                map2.put(word.charAt(i), pattern.charAt(i));
                i++;
            }
            if (i == word.length()) res.add(word);

        }
        return res;
    }
}
